import { createRouter, createWebHistory } from 'vue-router'
import useUserStore from '@/stores/modules/user'
const router = createRouter({
  // 路由基地址import.meta.env.BASE_URL
  // vite.config.ts文件   base: '/abc', 项目的路由地址中就会有 /abc 切换路由的时候  /abc不变的
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/index.vue'),
      meta: { title: '登录页面' }
    },
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: '/article',
          component: () => import('@/views/article/index.vue'),
          meta: { title: '健康百科' }
        },
        {
          path: '/notify',
          component: () => import('@/views/notify/index.vue'),
          meta: { title: '消息通知' }
        },
        {
          path: '/user',
          component: () => import('@/views/user/index.vue'),
          meta: { title: '个人中心' }
        }
      ]
    },
    {
      path: '/user/patient',
      component: () => import('@/views/user/PatientPage.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/consult/ConsultFast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/consult/ConsultIllness.vue'),
      meta: { title: '图文问诊' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/consult/ConsultPay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/room',
      component: () => import('@/views/room/index.vue'),
      meta: { title: '问诊室' },
      // 进入当前这个路由的时候, 查询支付结果是否为true
      beforeEnter: (to) => {
        // console.log(to)
        if (to.query.payResult === 'false') {
          return '/user/consult'
        }
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/user/ConsultPage.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/user/ConsultDetail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/order/orderPay.vue'),
      meta: { title: '药品支付' }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/order/OrderPayResult.vue'),
      meta: { title: '药品支付结果' }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/order/OrderDetail.vue'),
      meta: { title: '药品订单详情' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/order/OrderLogistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/login/login-callback.vue'),
      meta: { title: 'QQ三方登录' }
    }
  ]
})
// vue2中 放行 强制拦截到某个页面都是 next()  next('/login')
// vue3中导航守卫参数中没有next函数 return false 禁止跳转  return '/login'
const whiteList = ['/login', '/login/callback']
router.beforeEach((to) => {
  // console.log('切换页面的时候 更新标题文本', to)
  document.title = '优医问诊' + to.meta.title
  const token = useUserStore().user?.token
  // 登录后禁止再回登录页面
  if (token && to.path === '/login') return '/'
  // 没登录不能访问非白名单页面  直接去登录
  if (!token && !whiteList.includes(to.path)) return '/login'
})
export default router

// const obj = { name: 'zs', age: 18 }

// const obj2: typeof obj = { name: 1, age: 12 }
